/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.sd.common.constants.DestoyType;
import com.jeesite.modules.sd.common.constants.GlobalEnum;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.entity.DocDestoyRecord;
import com.jeesite.modules.sd.service.DocDeliverRecordService;
import com.jeesite.modules.sd.service.DocDestoyRecordService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.entity.DocDestroyTask;
import com.jeesite.modules.sd.service.DocDestroyTaskService;

import java.util.Date;
import java.util.List;

/**
 * 文件销毁任务Controller
 * @author ysj
 * @version 2020-08-07
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/docDestroyTask")
public class DocDestroyTaskController extends BaseController {

	@Autowired
	private DocDestroyTaskService docDestroyTaskService;
	@Autowired
	private UserService userService;
	@Autowired
	private DocDeliverRecordService recordService;
	@Autowired
	private DocDestroyTaskService taskService;
	@Autowired
	private DocDestoyRecordService destoyRecordService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DocDestroyTask get(String id, boolean isNewRecord) {
		return docDestroyTaskService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sd:docDestroyTask:view")
	@RequestMapping(value = {"list", ""})
	public String list(DocDestroyTask docDestroyTask, Model model) {
		model.addAttribute("docDestroyTask", docDestroyTask);
		return "modules/sd/docDestroyTaskList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sd:docDestroyTask:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DocDestroyTask> listData(DocDestroyTask docDestroyTask, HttpServletRequest request, HttpServletResponse response) {
		docDestroyTask.setPage(new Page<>(request, response));
		Page<DocDestroyTask> page = docDestroyTaskService.findPage(docDestroyTask);
		List<DocDestroyTask> list = page.getList();
		if (CollectionUtils.isNotEmpty(list)){
			list.forEach(de->{
				DocDestoyRecord record=new DocDestoyRecord();
				record.setDestoyId(de.getId());
				long count = destoyRecordService.findCount(record);
				de.setFileCount(String.valueOf(count));
			});
		}

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sd:docDestroyTask:view")
	@RequestMapping(value = "form")
	public String form(DocDestroyTask docDestroyTask, Model model) {
		model.addAttribute("docDestroyTask", docDestroyTask);
		return "modules/sd/docDestroyTaskForm";
	}

	/**
	 * 查找销毁数据
	 * @return
	 */
	@RequiresPermissions("sd:docDestroyTask:view")
	@RequestMapping(value = "getInfors")
	@ResponseBody
	public Page<DocDeliverRecord> getInfors(DocDeliverRecord docDeliverRecord,HttpServletRequest request, HttpServletResponse response) {

		docDeliverRecord.setPage(new Page<>(request, response));
		docDeliverRecord.getSqlMap().getWhere().andBracket("deliver_type",QueryType.EQ, GlobalEnum.RECORD_DELIVER_TYPE_DELIVER.getKey(),1).or("deliver_type",QueryType.EQ,GlobalEnum.RECORD_DELIVER_TYPE_SEND.getKey(),2).endBracket();
		docDeliverRecord.getSqlMap().getWhere().and("take_date", QueryType.IS_NOT_NULL,null);
		docDeliverRecord.getSqlMap().getWhere().and("return_date", QueryType.IS_NOT_NULL,null);
		docDeliverRecord.getSqlMap().getWhere().and("destroy_task_id",QueryType.IS_NULL,null);
		Page<DocDeliverRecord> page = recordService.findPage(docDeliverRecord);
		return page;
	}

	/**
	 *跳转到销毁任务页面
	 * @return
	 */
	@PostMapping(value = "getInfor")
	public String getInfor(DocDeliverRecord deliverRecord, Model model) {

		model.addAttribute("docDeliverRecord",deliverRecord);
		return "modules/sd/docDestroyTaskFile";
	}





	/**
	 * 銷毀文件
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("sd:docDestroyTask:edit")
	@PostMapping(value = "destoyFiles")
	@ResponseBody
	public String destoyFiles(String[] ids,String destroyId) {
        if (ids.length==0){
			return renderResult(Global.FALSE, text("请选择文件"));
		}
        docDestroyTaskService.addDestotyFile(ids,destroyId);

		return renderResult(Global.TRUE, text("保存文件销毁任务成功！"));
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sd:docDestroyTask:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DocDestroyTask docDestroyTask) {
		docDestroyTask.setDestoyType(DestoyType.DESTOY_TYPE_DEFAL.getValue());
		try{
			docDestroyTaskService.save(docDestroyTask);
		}catch (Exception e){
			return renderResult(Global.FALSE, text("销毁文件保存失败！"));
		}

		return renderResult(Global.TRUE, text("销毁文件保存成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("sd:docDestroyTask:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DocDestroyTask docDestroyTask) {
		docDestroyTaskService.delete(docDestroyTask);
		return renderResult(Global.TRUE, text("删除文件销毁任务成功！"));
	}
	
}